import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";

const useGatewayType = () => {
    const { remoteData } = useModel("global");
    const { remoteOrganizationInfo } = useModel("organization.info");
    const { baseInfo } = useModel("person.info");

    const state = useReactive({
        dataLoading: false,
        submitLoading: false,
        gatewayTypes: [],
        applications: [],
        totalCount: 0,
    });

    const loadGatewayTypes = async () => {
        state.dataLoading = true;
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/developer/resource/getOrganizationGatewayTypes",
            params: {
                uniformSocialCreditCode: remoteOrganizationInfo.uniformSocialCreditCode,
            },
        });
        state.gatewayTypes = res || [];
        state.dataLoading = false;
    };

    useEffect(() => {
        loadGatewayTypes();
    }, []);

    const loadApplications = async ({ pageSize, page, typeName }) => {
        state.dataLoading = true;

        state.dataLoading = false;
    };

    const insertGatewayType = async (values, callback) => {
        state.submitLoading = true;
        values.creater = { uuid: baseInfo.uuid, name: baseInfo.realName, time: new Date().getUTCTime() };
        values.submiter = { uniformSocialCreditCode: remoteOrganizationInfo.uniformSocialCreditCode, name: remoteOrganizationInfo.fullName };
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/developer/resource/insertOrganizationGatewayType",
            params: values,
        });
        res && callback && callback(res);
        state.submitLoading = false;
    };

    const updateGatewayType = async (values, callback) => {
        state.submitLoading = true;
        values.updater = { uuid: baseInfo.uuid, name: baseInfo.realName, time: new Date().getUTCTime() };
        values.submiter = { uniformSocialCreditCode: remoteOrganizationInfo.uniformSocialCreditCode, name: remoteOrganizationInfo.fullName };
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/developer/resource/updateOrganizationGatewayType",
            params: values,
        });
        res && callback && callback(res);
        state.submitLoading = false;
    };

    const deleteGatewayType = async (gatewayTypeName, callback) => {
        state.submitLoading = true;
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/developer/resource/deleteOrganizationGatewayType",
            params: {
                uniformSocialCreditCode: remoteOrganizationInfo.uniformSocialCreditCode,
                gatewayTypeName,
            },
        });
        res && callback && callback(res);
        state.submitLoading = false;
    };

    const updateIsStop = async (gatewayTypeName, isStop, callback) => {
        state.submitLoading = true;
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/developer/resource/updateOrganizationGatewayTypeIsStopStatus",
            params: {
                uniformSocialCreditCode: remoteOrganizationInfo.uniformSocialCreditCode,
                gatewayTypeName,
                isStop,
            },
        });
        res && callback && callback(res);
        state.submitLoading = false;
    };

    return { ...state, loadGatewayTypes, loadApplications, insertGatewayType, updateGatewayType, deleteGatewayType, updateIsStop };
};

export default useGatewayType;
